﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/Employee.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

$(document).ready(function () {
     BindDepartment();
     $("#btnNext").bind("click", Save);
     $("#btnDel").bind("click", Del);
    var ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        $("#btnDel").show();
        BindData(ID,$("#MainContent_Category").val());
    }
});

function BindDepartment()
{
        $.ajax({
            url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            success: function (data) {
            Util.unblockUI();
            var ret=$.parseJSON(data).Departments;
            var selects= $("#Department");
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var obj=$.parseJSON(data);
            Util.freshPage("EmployeeList.aspx?ID="+obj.MSG+"&Category="+$("#MainContent_Category").val());
        }
    });
}

function Del()
{
    if(!confirm("确定删除?"))
    {
       return;
    }

    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val())
    }
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:"removeData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在删除数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("EmployeeList.aspx?Category="+$("#MainContent_Category").val());
        }
    });
}

function GetData() {
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "DepID":encodeURIComponent($("#Department").find("option:selected").val()),   
       "Name": encodeURIComponent($("#Name").val()),
       "Category": encodeURIComponent($("#MainContent_Category").val()),
       "Gender": encodeURIComponent($("input[name=Gender]").filter(':checked').val()),
       "Education": encodeURIComponent($("#Education").find("option:selected").text()),
       "Title": encodeURIComponent($("#Title").val()),
       "BirthDate": encodeURIComponent($("#BirthDate").val()),
       "TelPhone": encodeURIComponent($("#TelPhone").val()),
       "CellPhone": encodeURIComponent($("#CellPhone").val()),
       "Email": encodeURIComponent($("#Email").val()),
       "IdentityNo": encodeURIComponent($("#IdentityNo").val()),
       "GraduateSchool": encodeURIComponent($("#GraduateSchool").val()),
       "WorkProfession":encodeURIComponent($("#WorkProfession").val()),
       "Profession":encodeURIComponent($("#Profession").val()),
       "Address":encodeURIComponent($("#Address").val()),
       "Job":encodeURIComponent($("#Job").val()),
       "JobExperience":encodeURIComponent($("#JobExperience").val()),
       "JobAchievement": encodeURIComponent($("#JobAchievement").val()),
       "Memo": encodeURIComponent($("#Memo").val()),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val())
    }

    return ret;
}

function IsValidate() {
    var msg="";
    if (Util.isEmpty($('#Name').val())) {
        msg="名称不能为空!";
    }
    else if ($('#Department').val()=='-1') {
        msg="请选择所属部门!";
    }
    else if (Util.isEmpty($('#Title').val())) {
        msg="请输入职称!";
    }

    if(Util.isEmpty(msg))
    {
        return true;
    }
    else
    {
        alert(msg);
        return false;
    }
}

function BindData(ID,Category) {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID,category:Category},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).Employees[0];
                $("#MainContent_ID").val(decodeURIComponent(obj.ID));
                $("#Name").val(decodeURIComponent(obj.Name));       
                $("#Title").val(decodeURIComponent(obj.Title));               
                $("#BirthDate").val(Util.changeDateFormat(decodeURIComponent(obj.BirthDate)));
                SelectedValue("Department",decodeURIComponent(obj.DepID));
                SelectedRadio("Gender",decodeURIComponent(obj.Gender));
                SelectedText("Education",decodeURIComponent(obj.Education));
                $("#TelPhone").val(decodeURIComponent(obj.TelPhone));
                $("#CellPhone").val(decodeURIComponent(obj.CellPhone));
                $("#Email").val(decodeURIComponent(obj.Email));
                $("#IdentityNo").val(decodeURIComponent(obj.IdentityNo));
                $("#GraduateSchool").val(decodeURIComponent(obj.GraduateSchool));
                $("#Profession").val(decodeURIComponent(obj.Profession));
                $("#WorkProfession").val(decodeURIComponent(obj.WorkProfession));
                $("#Address").val(decodeURIComponent(obj.Address));
                $("#JobExperience").val(decodeURIComponent(obj.JobExperience));
                $("#JobAchievement").val(decodeURIComponent(obj.JobAchievement));
                $("#Memo").val(decodeURIComponent(obj.Memo));
            }
        });
}